#ifndef __MCS__LOOP_ELEMENT__HPP
#define __MCS__LOOP_ELEMENT__HPP

#include "Element.hpp"
#include "Condition.hpp"

class LoopElement : public Element
{
public:
	LoopElement(SourceLocation const & location, bool precondition, ptr<Expression> condition, ptr<Element> body)
		: Element(location)
		, precondition_(precondition)
		, condition_(condition)
		, body_(body)
	{}

	virtual void init(Block * parentBlock)
	{
		Element::init(parentBlock);
		condition_.init(parentBlock);
		body_->init(parentBlock);
	}

	virtual void execute() const
	{
		if(precondition_ && !condition_.check()) return;
		
		do {
			body_->execute();
		} while(condition_.check());
	}
private:
	bool precondition_;
	Condition condition_;
	ptr<Element> body_;
};

#endif //__MCS__LOOP_ELEMENT__HPP
